package sys.knot.interpreter;

import sys.knot.model.ResultPart;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GeneralInterpreter implements ResultSetInterpreter {
    private List<String> headers = new ArrayList<>();
    private String dataSourceName;
    private String queryName;

    public void addMetaData(String dataSourceName, String queryName, ResultSetMetaData metaData) throws SQLException {
        for (int i=1; i <= metaData.getColumnCount(); i++) {
            headers.add(metaData.getColumnName(i));
        }
        this.dataSourceName = dataSourceName;
        this.queryName = queryName;
    }
    public ResultPart interpretRow(ResultSet resultSet) throws SQLException {
        ResultPart resultPart = new ResultPart();
        for(String header : headers) {
            resultPart.put(header, resultSet.getString(header));
        }
        resultPart.put("datasource", dataSourceName);
        resultPart.put("query", queryName);
        return resultPart;
    }

    @Override
    public String toString() {
        return "GeneralInterpreter";
    }
}
